package com.coder520.foregroundservicestudy;

import android.app.IntentService;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

/**
 * Created by huang on 2017/7/16.
 */

public class ForegroundService extends Service {
    public static final String ACTION_START = "com.coder520.foregroundservicestudy_START";
    public static final String ACTION_SWITCH = "com.coder520.foregroundservicestudy_SWITCH";
    public static final String ACTION_STOP = "com.coder520.foregroundservicestudy_stop";
    private static final String TAG = ForegroundService.class.getSimpleName();
    private MyThread mThread;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "coder520 onStartCommand called : intent.action = "
                + intent == null ? null : intent.getAction());
        if (intent != null && !TextUtils.isEmpty(intent.getAction())) {
            String action = intent.getAction();
            switch (action) {
                case ACTION_START:
                    mThread = new MyThread();
                    mThread.start();
                    break;
                case ACTION_SWITCH:
                    Intent serviceIntent = new Intent(this, ForegroundService.class);
                    serviceIntent.setAction(ForegroundService.ACTION_STOP);
                    PendingIntent pendingIntent = PendingIntent.getService(
                            this, 1, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    Notification notification = new Notification.Builder(this)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("我是一个前台service")
                            .setContentText("我是一个前台service content")
                            .setContentIntent(pendingIntent)
                            .build();
                    startForeground(1, notification);
                    break;
                case ACTION_STOP:
                    if (mThread == null || !mThread.isAlive()) {
                        break;
                    }
                    mThread.stopThread();
                    stopForeground(true);
                    break;
                default:
                    break;
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "coder520 onDestroy called ");
        super.onDestroy();
    }

    private class MyThread extends Thread {
        int count = 0;
        private boolean mStoped = false;
        private Object mLock = new Object();

        public void stopThread() {
            synchronized (mLock) {
                mStoped = true;
            }
        }

        @Override
        public void run() {
            while(!mStoped) {
                Log.d(TAG, "coder520 current count is :" + count++);
                try {
                    synchronized (mLock) {
                        mLock.wait(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
